#define _CRT_SECURE_NO_WARNINGS  1
#include<vector>
#include "RBTree.h"
#include "AVLTree.h"
// 测试代码
void TestRBTree1()
{
	RBTree<int, int> t;
	// 常规的测试用例
	int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
	// 特殊的带有双旋场景的测试用例
	//int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
	for (auto e : a)
	{
		t.Insert({ e, e });
	}
	t.InOrder();
	cout << t.IsBalance() << endl;

}
// 插入一堆随机值，测试平衡，顺便测试一下高度和性能等
void TestRBTree2()
{
	const int N = 1000000;
	vector<int> v;
	v.reserve(N);
	srand(time(0));
	for (size_t i = 0; i < N; i++)
	{
		v.push_back(i);
		//v.push_back(rand() + i);
	}
	size_t begin2 = clock();
	RBTree<int, int> t;
	for (auto e : v)
	{
		t.Insert(make_pair(e, e));
	}
	size_t end2 = clock();
	cout << "RBTree:Insert:" << end2 - begin2 << endl;
	cout << t.IsBalance() << endl;
	cout << "RBTree:Height:" << t.Height() << endl;
	cout << "RBTree:Size:" << t.Size() << endl;
	size_t begin1 = clock();
	// 确定在的值
	/*for (auto e : v)
	{
		t.Find(e);
	}*/
	// 随机值
	for (size_t i = 0; i < N; i++)
	{
		//t.Find((rand() + i));
		t.Find(i);
	}
	size_t end1 = clock();
	cout << "RBTree:Find:" << end1 - begin1 << endl;

	cout << endl;
}
void TestAVLTree2()
{
	const int N = 1000000;
	vector<int> v;
	v.reserve(N);
	srand(time(0));
	for (size_t i = 0; i < N; i++)
	{
		v.push_back(i);
		//v.push_back(rand() + i);
	}
	size_t begin2 = clock();
	AVLTree<int, int> t;
	for (auto e : v)
	{
		t.Insert(make_pair(e, e));
	}
	size_t end2 = clock();
	cout << "AVLTree:Insert:" << end2 - begin2 << endl;
	cout << t.IsBalanceTree() << endl;
	cout << "AVLTree:Height:" << t.Height() << endl;
	cout << "AVLTree:Size:" << t.Size() << endl;
	size_t begin1 = clock();
	// 确定在的值
	/*for (auto e : v)
	{
		t.Find(e);
	}*/
	// 随机值
	for (size_t i = 0; i < N; i++)
	{
		//t.Find((rand() + i));
		t.Find(i);
	}
	size_t end1 = clock();
	cout << "AVLTree:Find:" << end1 - begin1 << endl;
}
void TestRBTreeErase()
{
	const int N = 1000000;
	vector<int> v;
	v.reserve(N);
	srand(time(0));
	for (size_t i = 0; i < N; i++)
	{
		v.push_back(rand() + i);
	}
	size_t begin2 = clock();
	RBTree<int, int> t;
	for (auto e : v)
	{
		t.Insert(make_pair(e, e));
	}
	size_t end2 = clock();
	cout << "RBTree:Insert:" << end2 - begin2 << endl;
	cout << t.IsBalance() << endl;
	cout << "RBTRee:Height:" << t.Height() << endl;
	cout << "RBTree:Size:" << t.Size() << endl;

	size_t begin1 = clock();
	// 随机删除500个数据
	/*for (int i = 0; i < 500; i++) {
		t.Erase(v[rand() + i]);
		if (!t.IsBalance()) {
			cout << "红黑树不平衡，删除节点有误" << endl;
			break;
		}
	}*/

	// 删除所有节点需要的时间
	for (int i = 0; i < v.size(); i++)
	{
		t.Erase(v[i]);
		/*if (!t.IsBalance()) {
			cout << "红黑树不平衡，删除节点有误" << endl;
			break;
		}*/
	}
	size_t end1 = clock();
	cout << t.IsBalance() << endl;
	cout << "RBTRee:Erase:" << end1 - begin1 << endl;
	cout << endl;
}
void TestAVLTreeErase()
{
	const int N = 1000000;
	vector<int> v;
	v.reserve(N);
	srand(time(0));
	for (size_t i = 0; i < N; i++)
	{
		v.push_back(rand() + i);
	}
	size_t begin2 = clock();
	AVLTree<int, int> t;
	for (auto e : v)
	{
		t.Insert(make_pair(e, e));
	}
	size_t end2 = clock();
	cout << "AVLTree:Insert:" << end2 - begin2 << endl;
	cout << t.IsBalanceTree() << endl;
	cout << "AVLTree:Height:" << t.Height() << endl;
	cout << "AVLTree:Size:" << t.Size() << endl;

	size_t begin1 = clock();
	//// 随机删除500个数据
	//for (int i = 0; i < 500; i++) {
	//	t.Erase(v[rand() + i]);
	//	if (!t.IsBalanceTree()) {
	//		cout << "AVL树不平衡，删除节点有误" << endl;
	//		break;
	//	}
	//}

	// 删除所有节点需要的时间
	for (int i = 0; i < v.size(); i++)
	{
		t.Erase(v[i]);
	}
	size_t end1 = clock();
	cout << t.IsBalanceTree() << endl;
	cout << "AVLTree:Erase:" << end1 - begin1 << endl;
}
int main()
{
	//TestRBTree1();
	//TestRBTree2();
	//TestAVLTree2();
	TestRBTreeErase();
	TestAVLTreeErase();
	return 0;
}